package com.qdaily.ui.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.ui.QDBaseActivity;
import com.qdaily.ui.R;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.entity.BaseEntity;
import com.qdaily.ui.ui.views.TitleBar;
import com.qdaily.ui.util.QDUtil;

/**
 * 找回密码
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/13/15
 * Time: 22:21
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class RetrievePasswordActivity extends QDBaseActivity implements View.OnClickListener {
    private TitleBar viewTitleHeaderBar;
    private android.widget.EditText editTextRetrievePassword;
    private android.widget.TextView textViewRetrievePasswordSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_password);
        initViewRes();
    }

    private void initViewRes() {
        this.textViewRetrievePasswordSend = (TextView) findViewById(R.id.textViewRetrievePasswordSend);
        this.editTextRetrievePassword = (EditText) findViewById(R.id.editTextRetrievePassword);
        this.viewTitleHeaderBar = (TitleBar) findViewById(R.id.viewTitleHeaderBar);
        this.viewTitleHeaderBar.setTitle(R.string.retrieve_password);
        this.viewTitleHeaderBar.onBack();

        this.textViewRetrievePasswordSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.textViewRetrievePasswordSend) {
            String email = editTextRetrievePassword.getText().toString();
            if (TextUtils.isEmpty(email)) {
                displayToast(R.string.please_entry_email);
                return;
            }

            if (!QDUtil.isEmail(email)) {
                displayToast(R.string.entry_email_is_wrong);
                return;
            }

            getRequestManager().requestfindPassword(email);
        }
    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);
        if (params instanceof NetConfigs.RequestfindPassword) {
            BaseEntity baseEntity = QDJsonUtil.Json2Object(response.toString(), BaseEntity.class);
            displayToast(baseEntity.getMeta().getMsg());
            if (baseEntity.getMeta().getStatus() == NetConfigs.HTTP_RESPONSE_200) {
                RetrievePasswordActivity.this.finish();
            }
        }
    }
}
